package com.party.web.biz.crowdfund;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.redis.StringJedis;
import com.party.common.utils.LangUtils;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.distributor.WithCount;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.web.biz.activity.ActivityBizService;
import com.party.web.biz.file.FileBizService;
import com.party.web.web.dto.output.crowdfund.ListForTargetOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 联合发起业务接口 Created by wei.li
 *
 * @date 2017/3/2 0002
 * @time 14:12
 */

@Service
public class RepresentBizService {
    @Autowired
    private StringJedis stringJedis;
    @Autowired
    private TargetBizService targetBizService;
    @Autowired
    private IDistributorRelationService distributorRelationService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private ICrowdfundEventService crowdfundEventService;
    @Autowired
    private FileBizService fileBizService;
    @Autowired
    private ActivityBizService activityBizService;

    @Autowired
    private CosBizService cosBizService;
    @Value("#{party['mic.url']}")
    private String micUrl;

    /**
     * 项目下联合发起
     *
     * @param page      分页参数
     * @param id        编号
     * @param withCount 统计实体
     * @return 联合发起列表
     */
    public List<ListForTargetOutput> listForTarget(Page page, WithCount withCount, String id) {
        if (null == withCount) {
            withCount = new WithCount();
        }
        withCount.setTargetId(id);
        List<WithCount> distributorRelationList = distributorRelationService.listWithCount(withCount, page);

        List<ListForTargetOutput> list = LangUtils.transform(distributorRelationList, new Function<WithCount, ListForTargetOutput>() {
            public ListForTargetOutput apply(WithCount distributorRelation) {
                ListForTargetOutput listForTargetOutput = ListForTargetOutput.transform(distributorRelation);
                listForTargetOutput.setFavorerNum(distributorRelation.getFavorerNum());
                listForTargetOutput.setActualAmount(distributorRelation.getFavorerAmount());
                listForTargetOutput.setCrowdfundNum(distributorRelation.getCrowdfundNum());
                String filePath = fileBizService.getUploadCIPath("represent",distributorRelation.getDistributorId());
                String codeUrl = cosBizService.getPreviewPictureUrl(filePath + distributorRelation.getId());
                String content = "project/represent_target.html?id="+id+"&parentId="+distributorRelation.getParentId()+"&distributorId="+distributorRelation.getDistributorId();
                listForTargetOutput.setQrCodeUrl(codeUrl);
                listForTargetOutput.setUrl(micUrl + content);
                return listForTargetOutput;
            }
        });
        return list;
    }

    /**
     * 根据事件编号查询
     *
     * @param page      分页参数
     * @param withCount 查询参数
     * @return 输出视图
     */
    public List<ListForTargetOutput> listForEvent(Page page, WithCount withCount) {
        List<WithCount> distributorRelationList = distributorRelationService.listWithCount(withCount, page);
        List<ListForTargetOutput> list = LangUtils.transform(distributorRelationList, new Function<WithCount, ListForTargetOutput>() {
            public ListForTargetOutput apply(WithCount distributorRelation) {
                ListForTargetOutput listForTargetOutput = ListForTargetOutput.transform(distributorRelation);
                listForTargetOutput.setFavorerNum(distributorRelation.getFavorerNum());
                listForTargetOutput.setActualAmount(distributorRelation.getFavorerAmount());
                listForTargetOutput.setCrowdfundNum(distributorRelation.getCrowdfundNum());
                return listForTargetOutput;
            }
        });
        return list;
    }

    /**
     * @param page
     * @param withCount
     * @param id        项目id
     * @return
     * @Author yifeng
     * @Description 项目的数据查看的联合发起人列表
     * @Date 10:59 2019/3/25
     **/
    public Map<String, Object> listForActivityCommon(Page page, WithCount withCount, String id) {
        page.setLimit(20);
        List<ListForTargetOutput> listForTargetOutputList = listForTarget(page, withCount, id);
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", listForTargetOutputList);
        map.put("page", page);
        map.putAll(activityBizService.putZipInfo(Constant.PRE_ZIP_URL + "represent" + id));
        map.putAll(targetBizService.getTargetData(id));
        return map;
    }

    /**
     * @param page
     * @param withCount
     * @return
     * @Author yifeng
     * @Description 项目的数据查看的联合发起人列表
     * @Date 10:59 2019/3/25
     **/
    public Map<String, Object> listForEventCommon(Page page, WithCount withCount) {
        List<ListForTargetOutput> listForTargetOutputList = listForEvent(page, withCount);
        //直接付款人数
        Integer directPayNum = projectService.dpCountForEvent(withCount.getEventId(),null);
        Integer projectNum = projectService.countForEvent(withCount.getEventId());
        CrowdfundEvent crowdfundEvent = crowdfundEventService.get(withCount.getEventId());
        Map<String, Object> map = Maps.newHashMap();
        map.put("crowdfundEvent", crowdfundEvent);
        map.put("list", listForTargetOutputList);
        map.put("projectNum", projectNum);
        map.put("page", page);
        map.put("directPayNum", directPayNum);
        return map;
    }
}
